<template>
    <div class="box">
        <header>
            <van-nav-bar title="地址列表" left-text="返回" left-arrow @click-left="$router.back()" />
        </header>
        <div class="content">
            <van-address-list v-model="chosenAddressId" :list="addresslist" default-tag-text="默认" @add="onAdd"
                @click-item="updateAddress" @edit="onEdit" />
        </div>
    </div>
</template>
<script>
import Vue from "vue";
import { NavBar, AddressList } from "vant";

import { UpOrderAddress, AddList } from '@/api/order'


Vue.use(AddressList);
Vue.use(NavBar);
import { Toast } from 'vant';

export default {
    data() {
        return {
            chosenAddressId: '1',
            addresslist: [],
        };
    },
    methods: {
        updateAddress(item) {
            console.log(item)
            item.time = this.$route.params.time
            UpOrderAddress(item).then(() => {
                this.$router.back()
                console.log(item);
            })
        },
        onAdd() {
            Toast('新增地址');
            this.$router.push('/addnew/' + this.$route.params.time)
        },
        onEdit(item, index) {
            // Toast('编辑地址:' + index);
        },
    },
    mounted() {
        AddList({
            userid: localStorage.getItem('userid')
        }).then(res => {
            // console.log(res.data);
            res.data.map((item, index) => {
                let str = ''
                item.province === item.city
                    ? str = item.province + item.county + item.addressDetail
                    : str = item.province + item.city + item.county + item.addressDetail
                item.address = str
                item.id = index + 1
            })
            this.addresslist = res.data
        })
    },
};
</script>
